<template>
  <div>
    <!-- 商品列表 -->
    <!-- 搜索框 -->
    <el-card>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input
            placeholder="搜索"
            v-model="pager.keyword"
            style="width: 500px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search"
            >搜索</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-circle-plus-outline"
            @click="handleAdd"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 列表框 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column fixed prop="id" label="Id" width="120">
      </el-table-column>
      <el-table-column prop="productName" label="商品名称" width="120">
      </el-table-column>
      <el-table-column prop="price" label="单价" width="120"> </el-table-column>
      <el-table-column prop="className" label="品类类型" width="120">
      </el-table-column>
      <el-table-column prop="brandName" label="品牌" width="120">
      </el-table-column>
      <el-table-column prop="storeName" label="店铺名称" width="120">
      </el-table-column>
      <!-- <el-table-column prop="priority" label="优先级" width="120">
      </el-table-column> -->
      <el-table-column prop="isActived" label="状态" width="120">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isActived"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeSwitchs(scope.$index, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createdTime" label="添加时间" width="120">
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.$index, scope.row)"
            type="text"
            size="small"
            icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加模态框 -->
    <!-- <el-dialog
      title="添加商品"
      :visible.sync="dialogFormVisible"
      style="overflow: hidden"
    >
      <el-form :model="formData" ref="elForm" :rules="rules">
        <el-form-item
          label="商品名称:"
          prop="productName"
          :label-width="formLabelWidth"
        >
          <el-input clearable v-model="formData.productName"></el-input>
        </el-form-item>
        <el-form-item label="单价:" prop="price" :label-width="formLabelWidth">
          <el-input clearable v-model="formData.price"></el-input>
        </el-form-item>
        <el-form-item
          label="品类类型:"
          prop="className"
          :label-width="formLabelWidth"
        >
          <el-select
            clearable
            v-model="formData.className"
            placeholder="请选择类型"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.value"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="店铺名称:"
          prop="storeName"
          :label-width="formLabelWidth"
        >
          <el-input clearable v-model="formData.storeName"></el-input>
        </el-form-item>
        <el-form-item
          label="品牌:"
          prop="brandName"
          :label-width="formLabelWidth"
        >
          <el-input clearable v-model="formData.brandName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handeleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog> -->

    <!-- 分页组件 -->

    <div class="block">
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handlePageIndexChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
        background
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
// import { use } from 'vue/types/umd'
import {
  getCommodity,
  modCommodity,
  deleCommodity
} from '../../api/productlist'
import router from '../../router'
import formatTime from '../../utils/formatTime'

export default {
  mounted () {
    this.fetchData(this.pager)
  },
  data () {
    return {
      tableData: [],
      formData: {
        id: 0,
        productName: '',
        brandName: '',
        price: '',
        className: '',
        storeName: ''
      },
      dialogFormVisible: false,
      formLabelWidth: '120px',
      pager: {
        pageIndex: 1,
        pageSize: 10,
        rowsTotal: 30,
        keyword: ''
      },
      rules: {
        productName: [
          {
            required: true,
            message: '商品名称不能为空！',
            trigger: ['red']
          }
        ],
        price: [
          {
            required: true,
            message: '单价不能为空！',
            trigger: ['red']
          }
        ],
        className: [
          {
            required: true,
            message: '店铺名称不能为空！',
            trigger: ['red']
          }
        ]
      }
    }
  },
  methods: {
    // 搜索
    search () {
      this.pager.pageIndex = 1
      this.fetchData(this.pager)
    },
    // 添加
    handleAdd () {
      router.push('/commodity/Add')
      // this.dialogFormVisible = true
      // this.formData.id = 0
      // this.formData.productName = ''
      // this.formData.price = ''
      // this.formData.className = ''
      // this.formData.storeName = ''
      // this.formData.brandName = ''
    },
    // swith状态
    async changeSwitchs (index, row) {
      console.log(index, row)
      const rw = row
      delete rw.createdTime
      delete rw.updatedTime
      const res = await modCommodity(row.id, row)
      console.log(res)
      if (res.data.code === 200) {
        this.$message.success('修改成功！')
      } else {
        this.$message.error('修改失败！')
      }
    },
    // 编辑
    handleClick () {
      this.$message.success('正在开发中，敬请期待 ^_^')
      // this.dialogFormVisible = true
      // this.formData.id = row.id
      // this.formData.productName = row.productName
      // this.formData.price = row.price
      // this.formData.className = row.className
      // this.formData.storeName = row.storeName
      // this.formData.brandName = row.brandName
      // this.formData.isActived = row.isActived

      // 编辑时获取当前数组中的下标
      // this.currentIndex = index
      // console.log(index, row)
    },
    async handleDelete (index, row) {
      // 删除时获得当前数组的下标
      this.currentIndex = index
      row.isDeleted = true

      const res = await deleCommodity(row.id, row)
      if (res.data.code === 200) {
        this.$message.success('删除成功')
        this.fetchData(this.pager)
      } else {
        row.isDeleted = false
        this.$message.error('删除失败')
      }
    },
    // 删除
    // handleDelete (index, row) {
    //   this.$confirm('此操作将永久删除该供应商, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   })
    //     .then(() => {
    //       this.tableData.splice(index, 1)
    //       this.$message({
    //         type: 'success',
    //         message: '删除成功!'
    //       })
    //     })
    //     .catch(() => {
    //       this.$message({
    //         type: 'info',
    //         message: '已取消删除'
    //       })
    //     })
    // },

    // 添加取消
    handeleCancel () {
      this.dialogFormVisible = false
    },
    // 模态框确定(无论添加还是修改后保存商品信息)

    // handleSave () {
    //   // 添加
    //   this.$refs.elForm.validate((valid) => {
    //     if (!valid) return

    //     // TODO 提交表单
    //     console.log('没有错误', this.formData)

    //     // 如果表单中的id为0，则认为是添加用户，反之则为是修改用户
    //     if (this.formData.id === 0) {
    //       // const us = this.formData

    //       modCommodity(this.formData.id, this.formData)
    //         .then((data) => {
    //           console.log(data)
    //           const obj = data.data.data
    //           obj.className = data.data.data.user.className
    //           obj.createdTime = formatTime(obj.createdTime)
    //           this.tableData.splice(this.currentIndex, 1, obj)
    //           this.dialogFormVisible = false
    //           this.$message.success(data.data.msg)
    //         })
    //         .catch((err) => {
    //           console.log(err)
    //         })
    //     }
    //   })
    // },

    // 当页面大小发生改变的时候，重新拉取数据

    // 分页
    handlePageSizeChange (val) {
      console.log(val)
      this.pager.pageSize = val
      this.fetchData(this.pager)
    },
    // 当页面代码发生改变的时候，重新拉取数据
    handlePageIndexChange (val) {
      console.log(val)
      this.pager.pageIndex = val
      this.fetchData(this.pager)
    },

    // 拉取后台数据并且带上分页
    fetchData (pager) {
      console.log(pager)
      getCommodity(pager).then((data) => {
        data.data.data.data.forEach((item) => {
          item.createdTime = formatTime(item.createdTime)
        })

        console.log(data.data.data.data)
        this.tableData = data.data.data.data
        const pager = data.data.data.pager
        pager.keyword = this.pager.keyword
        this.pager = pager
      })
    }
  }
}
</script>

<style>
.block {
  text-align: center;
  margin-top: 15px;
}
</style>
